<?php
namespace Web\Controller;
use Think\Controller;
use Web\Logic\MenuLogic;
use Web\Logic\IndexLogic;
use Web\Logic\CommonLogic;
class IndexApiController extends BaseController {

    public function test(){
        $list = getProjectControl(11);
        dump($list);
        $device = getDeviceControl(1);
        dump($device);
    }

    function getSumApi(){
        $data = array();
        //1.1 室外参数
        $data['out'] = $this->getTempRhIndex(0);
        //1.2 冷机
        $data['cooler'] = $this->getCooler(0);

         //1.3 空调
        $data['ahu'] = $this->getAhuSum(0);
        //1.3日志
        $data['log'] = $this->getLog(0);
        //1,4 警告
       $data['warning'] = $this->getWarning(0);

        //1,5 能耗
       $data['energy'] = $this->getEngrgyH(0);
        $data['energy_today'] = $this->getEngrgyDay(0);

        //1.6 中庭检测数据
       $data['stall_temp'] =json_decode('{"co2":"485.0","pm25":"19.8","lux":"***","list":[{"data":[11,12.5,17,12.4,18.9,20.5,23.7,22.4,23.8,22.9,11.9,23.9,18.9,17.9],"type":"bar"}]}',true);

        //1.7 物业管理
        $data['realty'] = $this->getRealty(0);

       // echo json_encode($data);

        $this->ajaxReturn($data,"json");


    }

    function getFee(){
            $start_time = "1574697600";
            $current_time = time();
            $cha = $current_time-$start_time;

            $rate = 203*10000*(0.1)/365/86400;
            $list['energy'] = round($rate*$cha,1);
            $list['fee'] = round($list['energy']*0.685,1);
            $this->ajaxReturn($list,"json");
    }

	function getTempRhIndex($is_json =1){

		$cpns = $data = $array = array();
		$default = ['RF01-B'=>'***', 'RF02-B'=>'***', 'RF03-B'=>'***'];

        $where['cpn_name'] = array('IN','RF01-B,RF02-B,RF03-B');
        //温度
        $where['parm_type'] = 544;
        $temp_list = M('cpn_latest_parm')->where($where)->getField("cpn_name,parm_value");
		$temp_list = array_filter($temp_list);
           //exit(M('cpn_latest_parm')->getLastSql());
		$data['temp'] = array_map(function($v){ return formatFloat($v, 1); }, $temp_list) ?: $default;
		//平均温度
		$array['temp'] = formatFloat(array_sum($temp_list) / count($temp_list), 1);

        //湿度
        $where['parm_type'] = 545;
        $rh_list = M('cpn_latest_parm')->where($where)->getField("cpn_name,parm_value");
		$rh_list = array_filter($rh_list);

		$data['rh'] = array_map(function($v){ return formatFloat($v, 1); }, $rh_list) ?: $default;
		//平均湿度
		$array['rh'] = formatFloat(array_sum($rh_list) / count($rh_list), 1);

        //照度
        $where['parm_type'] = 546;
        $list = M('cpn_latest_parm')->where($where)->getField("cpn_name,parm_value");
		$list = array_filter($list);
		$data['lux'] = array_map(function($v){ return formatFloat($v, 1); }, $list) ?: $default;
		//平均照度
		$array['lux'] = formatFloat(array_sum($list) / count($list), 1);

        //PM2.5
        $where['parm_type'] = 551;
        $list = M('cpn_latest_parm')->where($where)->getField("cpn_name,parm_value");
		$list = array_filter($list);
		$data['pm25'] = array_map(function($v){ return formatFloat($v, 1); }, $list) ?: $default;
		//平均PM25
		$array['pm25'] = formatFloat(array_sum($list) / count($list), 1);

        //湿球温度
		$tmp = array();
		foreach($temp_list as $cpn_name=>$vv){
			$tmp[$cpn_name] = getWetBulbTemperature($vv, $rh_list[$cpn_name]) ?: '***';
		}

		$data['wet-bulb'] = $tmp;
		$tmp = array_filter($tmp);
		$array['wet-bulb'] = formatFloat(array_sum($tmp) / count($tmp), 1);

		$names = array('RF01-B', 'RF02-B', 'RF03-B');
		foreach($names as $name){
			$tmp = array();
			foreach($data as $k=>$v){
				$tmp['cpn'] = $name;
				$tmp[$k] = isset($v[$name]) ? $v[$name] : '***';
			}
			//判断是否所有值都为空
			$num = 0;
			foreach($tmp as $vv){
				if($vv['temp'] == '***') $num++;
				if($vv['rh'] == '***') $num++;
				if($vv['lux'] == '***') $num++;
				if($vv['pm25'] == '***') $num++;
				if($vv['wet-bulb'] == '***') $num++;
			}

			if($num != 5) $cpns[] = $tmp;
		}

		if(empty($cpns)){
			$cpns[] = array(
				'cpn'=>'cpnname',
				'temp'=>'***',
				'rh'=>'***',
				'lux'=>'***',
				'pm25'=>'***',
				'wet-bulb'=>'***',
			);
		}

		$array['cpns'] = $cpns;

        if($is_json == 0){
            return $array;
        }
        $this->ajaxReturn($array,"json");


    }

    //1.1 室外参数
    function getTempRh($is_json =1){
        $where['cpn_name'] ='RF01-B';
        //温度
        $where['parm_type'] = 544;
        $list = M('cpn_latest_parm')->where($where)->getField("parm_value");
          // echo M('cpn_latest_parm')->getLastSql();
        $array['temp'] = formatFloat($list,1);
        //湿度
        $where['parm_type'] = 545;
        $list = M('cpn_latest_parm')->where($where)->getField("parm_value");
        $array['rh'] = formatFloat($list,1);

        //照度
        $where['parm_type'] = 546;
        $list = M('cpn_latest_parm')->where($where)->getField("parm_value");
        $array['lux'] = formatFloat($list,1);

        //PM2.5
        $where['parm_type'] = 551;
        $list = M('cpn_latest_parm')->where($where)->getField("parm_value");
        $array['pm25'] = formatFloat($list,1);

        //湿球温度
        $array['wet-bulb'] = getWetBulbTemperature($array['temp'],$array['rh']);

        if($is_json == 0){
            return $array;
        }
        $this->ajaxReturn($array,"json");


    }

    /**
    * 显示报警日志
    *
    */
    public function getWarning($is_json =1){

        $role_id = session('roleid');
        $admin_role_id = C('ADMIN_ROLE');
        $admin_role = explode(",",$admin_role_id);
        $where = array();
        $where['finish'] = 0;
        if (!in_array("1000", $this->mFunction_list)) {
            $where['is_hidden'] = 0;
        }


        $count_where['act_type'] =array('egt',10);
        $count_where['finish'] = 0;
        $general_count = M('cpn_warning')->where($count_where)->count();

        $count_where['act_type'] =array('lt',10);
        $serious_count = M('cpn_warning')->where($count_where)->count();

        $list = M('cpn_warning')
            ->field(true)
            ->where($where)
            ->order('id desc')
            ->limit(10)
            ->select();

        $return =array();
        $return['general_count'] = $general_count;
        $return['serious_count'] = $serious_count;
        $return['list'] = $list;
         if($is_json == 0){
            return $return;
        }
        $this->ajaxReturn($return,"json");
    }

    /**
    * 获取日志信息
    *
    */
    public function getLog($is_json =1){

        $where = array();
        if (!in_array("1000", $this->mFunction_list)) {
            $where['l.is_hidden'] = 0;
        }

        $list = M('p_user_operation_log')->alias('l')
            ->join('ly_member m ON l.userid=m.userid')
            ->field('m.username,l.*')
            ->order('id desc')
            ->where($where)
            ->limit(8)
            ->select();
        if($is_json == 0){
            return $list;
        }
        $this->ajaxReturn($list,"json");
    }


    /**
    * 获取冷站信息
    *
    */
    function getCooler($is_json =1){


          //获取总制冷量
          $where_cool ="((parm_type = 545 and cpn_type = 32)) and parm_state in(1,2)" ;
          $cool = M('cpn_latest_parm')->where($where_cool)->getField('sum(parm_value) as  cool');
          $list['cool'] =  round($cool,2);
         // echo M('cpn_latest_parm')->getLastSql();

          //获取总功率
          $power_where ="((parm_type = 544 and cpn_type = 36) or ( parm_type= 592 and cpn_type = 37) or (parm_type = 544 and cpn_type = 32)) and parm_state in(1,2)" ;
          $power = M('cpn_latest_parm')->where($power_where)->getField('sum(parm_value) as  power');
          $list['power'] =  round($power,2);

          //获取额定总功率
          $where_cool_set ="((parm_type = 785 and cpn_type = 36) or ( parm_type= 785 and cpn_type = 37) or (parm_type = 785 and cpn_type = 32)) and parm_state in(1,2)" ;
          $cool_set = M('cpn_latest_parm')->where($where_cool_set)->getField('sum(parm_value) as  cool_set');
          $list['cool_set'] =  round($cool_set,2);
          //echo   M('cpn_latest_parm')->getLastSql();

          if($list['cool_set'] == 0 || $list['power']==0 ){
              $list['cooler_cop'] =  0.0;
          }else{
               $list['cooler_cop'] =  round($list['cool']/$list['power'],1);
          }
         //$list['cooler_cop'] = 4.1;
          //$list['cool_set'] = 1;
          //$list['cool'] = 1;

          //取得开机台数
          $where_count['parm_type'] = 512;
          $where_count['parm_state'] = array('in','1,2');
          $where_count['parm_value'] = array('in','1,2,3,4,5,6,7');
          $where_count['cpn_type'] = 32;
          $count=  M('cpn_latest_parm')->where($where_count)->count();
          $list['cooler_open_count'] =  intval($count);
           if($is_json == 0){
            return $list;
        }
         echo json_encode($list);
     }

     /**
    * 获取空调系统综合统计
    *
    */
    public function getAhuSum($is_json = 1){


        //获取所有的空调设备
        //冷却水进口压力出口压力
        $where =array();
        $where['cpn.cpn_type'] = 48;
        $list = M('cpn')
         ->alias('cpn')
         ->join('LEFT JOIN '.C('DB_PREFIX').'cpn_latest_parm parm on cpn.cpn_name = parm.cpn_name and  parm.parm_type = 0x208')
         ->field('cpn.group_id,cpn.cpn_name,parm.parm_value as is_open')
         ->order('cpn.group_id,cpn.cpn_name')
        ->where($where)
        ->select();

        //dump($list);

       foreach($list as $k =>$v){
            $showlist[$v['group_id']]['sum_count']++;
            $showlist[$v['group_id']]['group_id'] = $v['group_id'];
            if(!isset($showlist[$v['group_id']]['open_sum'])){
               $showlist[$v['group_id']]['open_sum'] = 0;;
            }
            if(!isset($showlist[$v['group_id']]['normal_sum'])){
               $showlist[$v['group_id']]['normal_sum'] = 0;;
            }
            if($v['is_open'] > 0){
                $showlist[$v['group_id']]['open_sum']++;
            }
            $warning = parent::getWarning($v['cpn_name']);
            if(count($warning) > 0 ){
                $showlist[$v['group_id']]['normal_sum']++;
            }



       }
       $showlist['2']['sum_count'] = $showlist['2']['sum_count']+$showlist['4']['sum_count'];
       $showlist['2']['open_sum'] = $showlist['2']['open_sum']+$showlist['4']['open_sum'];
       $showlist['2']['normal_sum'] = $showlist['2']['normal_sum']+$showlist['4']['normal_sum'];
       unset($showlist['4']);
       $return_list = array_values($showlist) ;
       if($is_json == 0){
            return $return_list;
       }

       $this->ajaxReturn(array_values($showlist),"json");
     }



      /**
    * 获取中庭环境监测数据
    *
    */
    public function getStallSum($is_json = 1){

        //取得温度
        $map['courts.postion'] = array('egt',1);

        $list = array();
        $data =  M('cpn_parm_plan_courts_param')
        ->alias('courts')
        ->join(C('DB_PREFIX')."cpn_latest_parm lt on lt.cpn_name = courts.cpn_name and lt.parm_type = 544 and parm_state in (1,2) " )
        ->field('courts.postion,lt.floor,avg(parm_value) as parm_value')
        ->where($map)
        ->group("courts.postion,lt.floor")
        ->order("courts.postion,lt.floor")
        ->select();


        foreach($data as $k =>$v){
           $list[$v['postion']][$v['floor']]= round($v['parm_value'],1);
        }
        $full_data = array();
        foreach($list as $k =>$v){


             $full_data[$k] = $this->getFullNumber($v);
        }
        $list = $full_data;

        $show_list = array();
        foreach($list as $k =>$v){
            $temp['data'] = $v;
            $temp['type'] = 'line';
            $show_list[] = $temp;
        }

         //取得CO2
        $map = array();
        $map['cpn_type'] = 16;
        $map['parm_state'] = array('in',array(1,2));
        $map['parm_type'] = 0x226;
        $map['floor'] = array('in',array(8));
        $co2  = M('cpn_latest_parm')->where($map)->field("IFNULL(round(avg(parm_value),1),'***') as p_value")->find();
        //echo M('cpn_latest_parm')->getLastSql();

        //取得pm2.5
        $map['cpn_type'] = 16;
        $map['parm_state'] = array('in',array(1,2));
        $map['parm_type'] = 0x227;
        $map['floor'] = array('in',array(8));
        $pm25  = M('cpn_latest_parm')->where($map)->field("IFNULL(round(avg(parm_value),1),'***') as p_value")->find();


         //室内照度
        $map['cpn_type'] = 16;
        $map['parm_state'] = array('in',array(1,2));
        $map['parm_type'] =     0x222;
        $map['floor'] = array('in',array(8));
        $lux  = M('cpn_latest_parm')->where($map)->field("IFNULL(round(avg(parm_value),1),'***') as p_value")->find();


        $show_data['co2'] = $co2['p_value'];
        $show_data['pm25'] = $pm25['p_value'];
        $show_data['lux'] = $lux['p_value'];
        $show_data['list'] = $show_list;


        if($is_json == 0){
            return $show_data;
        }
        $this->ajaxReturn($show_data,"json");
    }

    function  getFullNumber($item){
         $temp = array();
         for($i = 4;$i <=10;$i++){
               if(isset($item[$i])){
                    $temp[] = round($item[$i],1);

               }else{
                    $temp[] = '-';
               }
         }
         return $temp;
    }



    /**
    * 获取能耗
    *
    */
    public function getEngrgyH($json = 1,$id=1){
       if($start == null){
           $start = date("Y-m-d",strtotime("-7 day"));
        }
        if($end == null){

           $end = date("Y-m-d");
        }
        $sub_map['es.dates']  = array('between',array($start, $end));


        $chart_value = array();
        $where['id'] =$id;
        $list = M('energy_group_sum')->where($where)->find();
        $show_list['title'] = $list['sum_name'];

        $series = array();

        for($i = 7;$i--;$i >=0){
            $day  = date("Y-m-d",strtotime("-".$i." day"));

            $date_list[] = $day;
            $data = $this->getEngrgyDay(0,$id,$day);
            foreach($data as $k =>$v){

                    $key = $v['name'];
                    $series[$key]['name'] = $v['name'];
                    $series[$key]['data'][] = $v['value'];
            }

        }
        $show_list['xAxis'] = $date_list;
        $show_list['series'] = array_values($series);

        if($is_json == 0){
            return $show_list;
         }


        $this->ajaxReturn($show_list,"json");



    }


     /**
    * 获取能耗
    *
    */
    public function getEngrgyDay($is_json = 1,$id = 1,$dates=0){

        if($dates == 0 ){
            $dates = date("Y-m-d");
        }

        //$sub_map['es.dates']  = $dates;



         $chart_value = array();
         $where['id'] = $id;
         $list = M('energy_group_sum')->where($where)->find();

         $show_list['title'] = $list['sum_name'];

         $energy_name = array();
         $date_list = array();

         //取得分组数据
         $sub_map['gp.sum_id'] = $list['id'];
         $sub = M('energy_group')->alias('gp')
         ->join('JOIN '.C('DB_PREFIX').'energy_group_list gl on gl.group_id = gp.id and gl.sum_id = gp.sum_id')
         ->join('LEFT JOIN '.C('DB_PREFIX')."energy_cpn_sum es on gl.cpn_name = es.cpn_name and es.dates = '$dates'")
         ->where($sub_map)
         ->group('gl.group_id')
         ->order('gl.group_id')
         ->field('energy_name as name,max(es.dates) as dates,sum(es.`values`) as sum_values')
         ->select();

         $show_list = array();
         foreach($sub as $k =>$v){
            $data = array();
            $data['name'] = $v['name'] ;
            $data['value'] = $v['sum_values']+0 ;
            $show_list[] =$data;
         }
         if($is_json == 0){
            return $show_list;
         }
         $this->ajaxReturn($show_list,"json");

    }


    public function getRealty(){

      $device = array();
      $wc = array();
      $stall = array();

      $where['fan.type_id'] = 1;
      $device_open = M('cpn_device_fan')
      ->alias("fan")
      ->join('LEFT JOIN '.C('DB_PREFIX')."cpn_latest_parm parm on fan.cpn_name = parm.cpn_name and fan.run_var = parm.parm_type and parm.parm_state in(1,2) ")
      ->field('count(fan.id) as sum_count,sum(case when  parm_value> 0 then 1 else 0 end ) as open_sum')
      ->where($where)->find();

      $where['fan.type_id'] = 2;
      $wc_open = M('cpn_device_fan')
      ->alias("fan")
      ->join('LEFT JOIN '.C('DB_PREFIX')."cpn_latest_parm parm on fan.cpn_name = parm.cpn_name and fan.run_var = parm.parm_type and parm.parm_state in(1,2)")
      ->field('count(fan.id) as sum_count,sum(case when  parm_value> 0 then 1 else 0 end ) as open_sum')
      ->where($where)->find();


      $where['fan.type_id'] = 3;
      $stall_open = M('cpn_device_fan')
      ->alias("fan")
      ->join('LEFT JOIN '.C('DB_PREFIX')."cpn_latest_parm parm on fan.cpn_name = parm.cpn_name and fan.run_var = parm.parm_type and parm.parm_state in(1,2)")
      ->field('count(fan.id) as sum_count,sum(case when  parm_value> 0 then 1 else 0 end ) as open_sum')
      ->where($where)->find();


      $list['device'] = $device_open;
      $list['wc'] = $wc_open;
      $list['stall'] = $stall_open;

      if($is_json == 0){
            return $list;
         }
         $this->ajaxReturn($list,"json");
      }



     public function getTask(){
      $list = M('')->query("SELECT e.username,cpn_name,create_time from ly_employee_inspection_ahu ahu
                INNER JOIN ly_e_employee  e on e.phone = ahu.tel_phone
                union
                SELECT e.username,l.sum_address as cpn_name,create_time from ly_employee_inspection_basic basic  INNER JOIN ly_cpn_location  l on basic.cpn_name = l.cpn_name
                INNER JOIN ly_e_employee  e on e.phone = basic.tel_phone
                order by create_time desc limit 10");

        $this->ajaxReturn($list,"json");
    }







    /**
    * 获取能耗柱形图
    *
    * @param mixed $start
    * @param mixed $end
    * @param mixed $id
    */
    public function getEngrgy($start= null,$end =  null,$id=1){
       if($start == null){
           $start = date("Y-m-d",strtotime("-7 day"));
        }

        if($end == null){

           $end = date("Y-m-d");
        }
        $sub_map['es.dates']  = array('between',array($start, $end));


         //先取得有多少个模块
        $show_array = array();
        $show_array[] =session(C('SESSION_ADMIN_ID'));
        //$where['user_id'] = array('in',$show_array);

         $chart_value = array();
         $where['id'] =$id;
         $list = M('energy_group_sum')->where($where)->find();
         foreach($list as $k =>$v){

             $chart_value_sub = array();
             $chart_value_sub['id'] = $v['id'];
             $chart_value_sub['title'] = $v['sum_name'];
             $chart_value_sub['key'] = 'date';
             $chart_value_sub['value'] = 'value';
             //取得分组数据
             $sub_map['gp.sum_id'] = $v['id'];
             $sub = M('energy_group')->alias('gp')
             ->join('JOIN '.C('DB_PREFIX').'energy_group_list gl on gl.group_id = gp.id and gl.sum_id = gp.sum_id')
             ->join('JOIN '.C('DB_PREFIX').'energy_cpn_sum es on gl.cpn_name = es.cpn_name')
             ->where($sub_map)
             ->group('gl.group_id,es.dates')
             ->order('gl.group_id,es.dates')
             ->field('energy_name as name,es.dates as "date",sum(es.`values`) as value')
             ->select();

             $chart_value_sub['list'] = $sub;
             $chart_value[] = $chart_value_sub;
         }
         $this->ajaxReturn($chart_value,"json");



    }

    /**
    * 获取能耗饼形图
    *
    */
    public function getEngrgyStatus(){

        $sub_map = array();
        //$sub_map['sums.dates']  = date("Y-m-d");

        $sub = M('energy_cpn_sum')->alias('sums')
         ->join('JOIN '.C('DB_PREFIX').'cpn cpn on cpn.cpn_name = sums.cpn_name')
         ->join('JOIN '.C('DB_PREFIX').'devices_class class on cpn.cpn_type = class.class_id')
         ->where($sub_map)
         ->group('class.class_id')
         ->order('class.class_id')
         ->field('class.class_name as name ,sum(sums.`values`) as sum_value')
         ->select();
         $this->ajaxReturn($sub,"json");



    }







    function getParmValue($type){
         $mod = M("");
        $rdata = $mod->query("select  avg(m.parm_value) as avgparam  from  `ly_cpn_latest_parm` m WHERE  m.parm_type = $type and m.parm_value >0");
        $retunValue['avg']  = round($rdata[0]['avgparam'],1)."";

        $maxdata = $mod->query("SELECT  cpn_name as max_name ,parm_value as max_parm  from  `ly_cpn_latest_parm` WHERE  parm_type = $type and parm_value >0 ORDER BY parm_value desc limit 1");
        $retunValue['max'] = round($maxdata[0]['max_parm'],1)."";
        $retunValue['maxname']= $maxdata[0]['max_name'];
        $mindata = $mod->query("SELECT  cpn_name as min_name ,parm_value as min_parm  from  `ly_cpn_latest_parm` WHERE  parm_type = $type and parm_value >0 ORDER BY parm_value asc limit 1");
         $retunValue['min'] = round($mindata[0]['min_parm'],1)."";
        $retunValue['minname']= $mindata[0]['min_name'];

         $countdata = $mod->query("SELECT parm_value,count(1) as counts from(select round(m.parm_value,2) as parm_value from `ly_cpn_latest_parm` m WHERE  m.parm_type = $type and m.parm_value >0 ) as b GROUP BY parm_value order by parm_value");
         $retunValue['counts'] = $countdata;
        return $retunValue;
    }




    function getLoaction(){
       $mod = M("");
       $rdata = $mod->query("select  floor,sum(m.parm_value) as sum_person  from  `ly_cpn_latest_parm` m WHERE m.cpn_type =16 and  m.parm_type = 2064 and m.parm_value >0 group by floor");
       $this->ajaxReturn($rdata,"json");


    }


    function getEngrgyValue($table){
         $mod = M($table);
         $arrayList = Array();
         $where['dates'] = date('Y-m-d');
         $air_sum = $mod->field('IFNULL(sum(`values`),0) as `values`')->find();
         $air_today = $mod->field('IFNULL(sum(`values`),0) as `values`')->where($where)->find();
         $arrayList['sum'] = round($air_sum['values']/1,1)."kWh";
         $arrayList['today'] = round($air_today['values']/1,1)."kWh";
         return $arrayList;

    }

    function getLight(){
        $where['cpn_name'] ='F6-S2';
        $where['parm_type'] =0x222;
        $list = M('cpn_latest_parm')->where($where)->getField("parm_value");
        $array['light'] = $list;
        echo json_encode($array);

    }



    //取得巡检工单，维修工单，排除故障
    public function getOrders(){
        //巡检工单
        $date = date('Y-m-d');
        $map['start'] = array('elt', $date);
        $map['end'] = array('egt', $date);

        $xunjian_total = M('e_events')->alias('e')
            ->join(C('DB_PREFIX').'e_day_task_events te ON e.task_id=te.task_id')
            ->join(C('DB_PREFIX').'e_event_template ti ON te.tid=ti.tid')
            ->where($map)
            ->count();

        //巡检工单完成
        $list = M('e_events')->alias('e')
            ->join(C('DB_PREFIX').'e_day_task_events te ON e.task_id=te.task_id')
            ->join(C('DB_PREFIX').'e_event_info i ON te.tid=i.tid')
            ->join(C('DB_PREFIX').'e_event_template ti ON i.tid=ti.tid')
            ->join(C('DB_PREFIX').'e_form f ON i.form_id=f.id')
            ->where($map)
            ->field('e.task_id,f.table_name,te.tid,ti.id as template_id')
            ->select();

        $table_names = array_column($list, 'table_name', 'tid');

        $tasks = array();
        foreach($list as $v){
            $tasks[$v['task_id']][] = array($v['tid'], $v['template_id']);

        }

        $xunjian_finish = 0;
        foreach($tasks as $k=>$v){
            foreach($v as $vv){
                $table_name = $table_names[$vv[0]];
                $cond['task_id'] = $k;
                $cond['template_id'] = $vv[1];
                $cond['DATE(create_time)'] = date('Y-m-d');
                $xunjian_finish += M('e_'.$table_name)->where($cond)->count();
            }
        }

        $xunjian_nofinish = $xunjian_total - $xunjian_finish;
        unset($list, $tasks, $map, $cond);

        //维修工单
        $map["FROM_UNIXTIME(date,'%Y-%m-%d')"] = date('Y-m-d');
        $weixiu_total = M('e_cpn_report')->where($map)->count();
        $map['finish'] = 1;
        $weixiu_finish = M('e_cpn_report')->where($map)->count();
        $weixiu_nofinish = $weixiu_total - $weixiu_finish;
        unset($map);

        //维保
        $date = date('Y-m-d');
        $map['start'] = array('elt', $date);
        $map['end'] = array('egt', $date);

        $weibao_total = M('m_events')->where($map)->count();
        $cond["FROM_UNIXTIME(create_time,'%Y-%m-%d')"] = date('Y-m-d');
        $weibao_finish = M('m_maintenance_log')->where($cond)->count();
        $weibao_nofinish = $weibao_total - $weibao_finish;


        $this->ajaxReturn(compact('xunjian_total', 'xunjian_finish', 'xunjian_nofinish', 'weixiu_total', 'weixiu_finish', 'weixiu_nofinish', 'weibao_total', 'weibao_finish', 'weibao_nofinish'));


    }







}